package club.kingyin.commons.utils;

import club.kingyin.commons.response.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ResponseUtils {

    public static DataBuffer createResponseBuffer(ServerHttpResponse response, Result result){
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        DataBuffer buffer = response.bufferFactory().wrap(JSON.toJSONString(result).getBytes());
        return buffer ;
    }

//    public static DataBuffer createResponseBuffer(ServerHttpResponse response, String result){
//        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//        DataBuffer buffer = response.bufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8));
//        return buffer ;
//    }

    public static String stampToTime(long s) throws Exception{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;

    }
}
